package org.example.view;

//import org.example.dao.IndexViewDao;

import org.example.dao.PublishDao;
import org.example.dao.UsersDao;
import org.example.pojo.Publish;
import org.example.utils.ALiYunOssUtils;
import org.example.utils.IOUtlis;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class PagingView {

    // 返回 publish_id
    public int showPaging(int page, int limit, int total, int choose, String userName, int tags, String title, String outline, int id) throws Exception {
        int index = 0;
        List<Publish> ls = null;
        if (choose == 1) { // 1.正常 显示文章
            ls = new PublishDao().listPage(--page, limit);
        } else if (choose == 2) {  // 2. 按用户 模糊查询 显示
            ls = new PublishDao().listPublishByUser(userName, --page, limit);
        } else if (choose == 3) { // 3. 按标签 模糊查询 显示
            ls = new PublishDao().listPublishByTags(tags, --page, limit);
        } else if (choose == 4) { //标题
            ls = new PublishDao().listPublishByTitle(title, --page, limit);
        } else if (choose == 5) {  //概要
            ls = new PublishDao().listPublishByOutline(outline, --page , limit);
        } else if (choose == 6) {  // 查看点赞的 文章
            ls = new PublishDao().listPublishByUserLike(id, --page, limit);
        } else if (choose == 7) { // 查看收藏的 文章
            ls = new PublishDao().listPublishByUserCollect(id, --page, limit);
        } else if (choose == 8) {  // 查看总的 转发的 文章
            ls = new PublishDao().listPublishByUserRetransmission(id, --page, limit);
        } else if (choose == 9) { // 查看评论的 文章
            ls = new PublishDao().listPublishByComment(id, --page, limit);
        }else if(choose == 10){  // 删除文章, 查看发布的文章
            ls = new PublishDao().listPublishByUser(id, --page, limit);
        }

        // 展示文章
        showList(ls);
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("当前页码：" + (int)(page+1) + " 总页码：" + (int) (Math.ceil(1.0 * total / limit)) + " 总记录数：" + total);
            System.out.println("请输入操作数：w：上一页 s: 下一页 序号：查看文章  0：返回");
            String cnt = scanner.next();
            switch (cnt) {
                //上一页
                case "w": {
                    if (page  <= 0) {
                        System.out.println("输入操作错误，请重新输入");
                        break;
                    }
                    //  当前页码为第一页，共 几页 ， 总记录数为。。。
//                    ls = new PublishDao().listPage(--page, limit);
                    if (choose == 1) { // 1.正常 显示文章
                        ls = new PublishDao().listPage(--page, limit);
                    } else if (choose == 2) {  // 2. 按用户 模糊查询 显示
                        ls = new PublishDao().listPublishByUser(userName, --page, limit);
                    } else if (choose == 3) { // 3. 按标签 模糊查询 显示
                        ls = new PublishDao().listPublishByTags(tags, --page, limit);
                    } else if (choose == 4) {
                        ls = new PublishDao().listPublishByTitle(title, --page, limit);
                    } else if (choose == 5) {
                        ls = new PublishDao().listPublishByOutline(outline, --page, limit);
                    } else if (choose == 6) {  // 查看点赞的 文章
                        ls = new PublishDao().listPublishByUserLike(id, --page, limit);
                    } else if (choose == 7) { // 查看收藏的 文章
                        ls = new PublishDao().listPublishByUserCollect(id, --page, limit);
                    } else if (choose == 8) {  // 查看总的 转发的 文章
                        ls = new PublishDao().listPublishByUserRetransmission(id, --page, limit);
                    } else if (choose == 9) {
                        ls = new PublishDao().listPublishByComment(id, --page, limit);
                    }else if(choose == 10){
                        ls = new PublishDao().listPublishByUser(id, --page, limit);
                    }
                    if(choose!=10)
                        showList(ls);
                    break;
                }
                //下一页
                case "s": {
                    if(page>=(int) (Math.ceil(1.0 * total / limit) - 1)){
                        System.out.println("输入操作错误，请重新输入");
                        break;
                    }
//                    ls=new PublishDao().listPage(++page, limit);
                    if (choose == 1) { // 1.正常 显示文章
                        ls = new PublishDao().listPage(++page, limit);
                    } else if (choose == 2) {  // 2. 按用户 模糊查询 显示
                        ls = new PublishDao().listPublishByUser(userName, ++page, limit);
                    } else if (choose == 3) { // 3. 按标签 模糊查询 显示
                        ls = new PublishDao().listPublishByTags(tags, ++page, limit);
                    } else if (choose == 4) {
                        ls = new PublishDao().listPublishByTitle(title, ++page, limit);
                    } else if (choose == 5) {
                        ls = new PublishDao().listPublishByOutline(outline, ++page, limit);
                    } else if (choose == 6) {  // 查看点赞的 文章
                        ls = new PublishDao().listPublishByUserLike(id, ++page, limit);
                    } else if (choose == 7) { // 查看收藏的 文章
                        ls = new PublishDao().listPublishByUserCollect(id, ++page, limit);
                    } else if (choose == 8) {  // 查看总的 转发的 文章
                        ls = new PublishDao().listPublishByUserRetransmission(id, ++page, limit);
                    } else if (choose == 9) {
                        ls = new PublishDao().listPublishByComment(id, ++page, limit);
                    }else if(choose == 10){
                        ls = new PublishDao().listPublishByUser(id, ++page, limit);
                    }
                    showList(ls);
                    break;
                }
                case "0": {
//                    Runtime.getRuntime().exec("cls");
                    return 0;
                }
                //序号查看文章
                default: {
                    index = Integer.parseInt(cnt);
                    try {
                        showPublish(Integer.parseInt(cnt));
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    return index;
                }
            }
        }
    }

    // 序号 查看文章
    public void showPublish(int index) throws SQLException, IOException {
        List<String> ls = new PublishDao().showIndexView(index);
        String url = ls.get(0);
        String objectName = ls.get(1);
        String downloadFile = new ALiYunOssUtils().downloadFile(url, objectName);
        new IOUtlis().getText(downloadFile);

    }

    //展示 文章
    public void showList(List<Publish> ls) throws Exception {
        System.out.println(String.format(
                "%-5s\t%-15s\t%-15s\t%-15s\t%-15s\t%-15s\t%-15s",
                "文章序号", "标题", "摘要", "发布者", "标签", "发布时间", "更新时间"
        ));
        for (Publish i : ls) {
//                       System.out.println(i.getId()+",\t\t"+i.getTitle()+",\t\t"+i.getOutline()+",\t\t"+i.getContent()+",\t\t"+i.getUserId()+",\t\t"+i.getCreateTime()+",\t\t"+i.getUpdateTime());
//                        String userName1 = new GetUserName().getUserName(i.getUserId());
            System.out.println(String.format("%-10s\t%-15s\t%-20s\t%-15s\t%-20s\t%-20s\t%-20s",
                    i.getId(),i.getTitle(), i.getOutline(), new UsersDao().getUserName(i.getUserId()), new MeneView().tag(i.getTags()),
                    i.getCreateTime(), i.getUpdateTime()
            ));
        }
        ls.clear();
    }

}










